package com.hwj.service.Impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hwj.entity.Permission;
import com.hwj.mapper.PermissionMapper;
import com.hwj.service.PermissionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class PermissionServiceImpl implements PermissionService {
    @Autowired
    PermissionMapper permissionMapper;

    @Override
    public Permission selectByPrimaryKey(Long id) {
        return permissionMapper.selectByPrimaryKey(id);
    }

    @Override
    public PageInfo<Permission> page(Permission permission, Integer pageSize, Integer pageNum) {
        if(permission==null)
            permission=new Permission();
        String permissionName = permission.getPermissionName();
        String permissionRealName = permission.getPermissionRealName();
        String path = permission.getPath();
        if(permissionName!=null){
            permissionName="%"+permissionName+"%";
            permission.setPermissionName(permissionName);
        }
        if(permissionRealName!=null){
            permissionRealName="%"+permissionRealName+"%";
            permission.setPermissionRealName(permissionRealName);
        }
        if(path!=null){
            path="%"+path+"%";
            permission.setPath(path);
        }
        if(pageNum!=null&&pageNum<=0){
            pageNum=1;
        }
        if(pageSize!=null&&pageSize<=0){
            pageSize=3;
        }
        if(pageNum!=null&&pageSize!=null) {
            PageHelper.startPage(pageNum, pageSize);
        }
        List<Permission> page = permissionMapper.page(permission);
        return new PageInfo<>(page);
    }

    @Override
    public int deleteByPrimaryKey(Long id) {
        return permissionMapper.deleteByPrimaryKey(id);
    }

    @Override
    public int insert(Permission record) {
        return permissionMapper.insert(record);
    }

    @Override
    public int insertSelective(Permission record) {
        return permissionMapper.insertSelective(record);
    }

    @Override
    public int updateByPrimaryKeySelective(Permission record) {
        return permissionMapper.updateByPrimaryKeySelective(record);
    }

    @Override
    public int updateByPrimaryKey(Permission record) {
        return permissionMapper.updateByPrimaryKey(record);
    }
}
